package com.hero.tweaks.util;

import java.util.Locale;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.hero.tweaks.R;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import com.htc.configuration.HtcWrapConfiguration;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;

import android.app.Activity;
import android.app.AlertDialog;

public class AxisPreferenceActivity extends PreferenceActivity {

	private String mFragmentClass;

	protected Context mContext;
	boolean mInLocalHeaderSwitch;
	private int mSeekbarProgress;
	private EditText settingText;
	
	private Context context;

	SharedPreferences mPreferences;

	Locale defaultLocale;

	public static ActionBarText mActionText;
	public static String AppTitle = "";
	static Window window;

	public void colorChanged(int color) {
		// TODO Auto-generated method stub

	}

	protected Context getActivity() {
		// TODO Auto-generated method stub
		return null;
	}

	protected String getStartingFragmentClass(Intent intent) {
		if (mFragmentClass != null) {
			return mFragmentClass;
		}

		String intentClass = intent.getComponent().getClassName();
		if (intentClass.equals(getClass().getName())) {
			return null;
		}

		return intentClass;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mInLocalHeaderSwitch = true;
		super.onCreate(savedInstanceState);
		mInLocalHeaderSwitch = false;
		
		// setup the actionbar
		context = this;
		SetupActionBar();
	}

	@Override
	protected void onPause() {
		// Set out transition
		overridePendingTransition(R.anim.hold_in, R.anim.translate_to_left_out);
		super.onPause();
	}

	@Override
	public boolean onPreferenceStartFragment(PreferenceFragment caller,
			Preference pref) {
		// Override the fragment title for Wallpaper settings
		int titleRes = pref.getTitleRes();

		startPreferencePanel(pref.getFragment(), pref.getExtras(), titleRes,
				null, null, 0);
		return true;
	}

	public void openDialog(int currentProgress, String title, final int min,
			final int max, final Preference pref, final String path,
			final String key) {
		Resources res = getActivity().getResources();
		String cancel = res.getString(R.string.cancel);
		String ok = res.getString(R.string.ok);
		LayoutInflater factory = LayoutInflater.from(getActivity());
		final View alphaDialog = factory.inflate(R.layout.seekbar_dialog, null);

		final SeekBar seekbar = (SeekBar) alphaDialog
				.findViewById(R.id.seek_bar);

		seekbar.setMax(max);
		seekbar.setProgress(currentProgress);

		settingText = (EditText) alphaDialog.findViewById(R.id.setting_text);
		settingText
				.setOnEditorActionListener(new TextView.OnEditorActionListener() {
					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_DONE) {
							int val = Integer.parseInt(settingText.getText()
									.toString());
							seekbar.setProgress(val);
							return true;
						}
						return false;
					}
				});
		settingText.setText(Integer.toString(currentProgress));
		settingText.addTextChangedListener(new TextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				try {
					int val = Integer.parseInt(s.toString());
					if (val > max) {
						s.replace(0, s.length(), Integer.toString(max));
						val = max;
					}
					seekbar.setProgress(val);
				} catch (NumberFormatException ex) {
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}
		});

		OnSeekBarChangeListener seekBarChangeListener = new OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekbar, int progress,
					boolean fromUser) {
				mSeekbarProgress = seekbar.getProgress();
				if (fromUser) {
					settingText.setText(Integer.toString(mSeekbarProgress));
				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekbar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekbar) {
			}
		};
		seekbar.setOnSeekBarChangeListener(seekBarChangeListener);

		new AlertDialog.Builder(getActivity())
				.setTitle(title)
				.setView(alphaDialog)
				.setNegativeButton(cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// nothing
							}
						})
				.setPositiveButton(ok, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						int val = Integer.parseInt(settingText.getText()
								.toString());
						if (val < min) {
							val = min;
						}
						seekbar.setProgress(val);
						int newProgress = seekbar.getProgress();
						pref.setSummary(Integer.toString(newProgress));
						new CMDProcessor().su.runWaitFor("busybox echo "
								+ newProgress + " > " + path);
						final SharedPreferences.Editor editor = mPreferences
								.edit();
						editor.putInt(key, newProgress);
						editor.commit();
					}
				}).create().show();
	}
	
	private void SetupActionBar() {
		ActionBarExt localActionBarExt = new ActionBarExt(this, getActionBar());
		localActionBarExt.setFullScreenEnabled(true);
		ActionBarContainer localActionBarContainer = localActionBarExt
				.getCustomContainer();
		mActionText = new ActionBarText(this);
		mActionText.setPrimaryText(AppTitle);
		localActionBarContainer.addCenterView(mActionText);
	}
}